const Service = require('egg').Service;
const crypto = require('crypto');

/** @namespace ctx.service.saveError */
class SService extends Service {
  /**
   * 保存错误记录
   * @param message
   * @return {Promise<void>}
   */
  async save(message) {
    const md5 = crypto.createHash('md5').update(message).digest('hex');
    const [error, isNew] = await this.app.model.Error.findOrCreate(
        {
          where: {md5},
          defaults: {message},
        });
    if (!isNew)
      error.increment('count');
  }
}

module.exports = SService;